﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace mapeditor
{
    static class Menu
    {
        const int taille_case = 34, largeur = 100, hauteur = 600, nbre_boutons = 9;

        public static Sprite[] boutons = new Sprite[nbre_boutons];
        public static Sprite bg, bgbutton;
        private static MouseState mouse;
        private static Rectangle rec1, rec2;

        public static void Initialize()
        {
            for (int i = 0; i < boutons.GetLength(0); i++)
            {
                boutons[i] = new Sprite();
            }

            bg = new Sprite();
            bgbutton = new Sprite();

            rec1 = Rectangle.Empty;
            rec2 = Rectangle.Empty;

            bgbutton.Position = new Vector2(-1, -1);
        }

        public static void LoadContent(ContentManager Content)
        {
            boutons[0].Load(Content, "caisse");
            boutons[1].Load(Content, "plateforme");
            boutons[2].Load(Content, "platg");
            boutons[3].Load(Content, "platd");
            boutons[4].Load(Content, "senti");
            boutons[5].Load(Content, "heli");
            boutons[6].Load(Content, "soldat");
            boutons[7].Load(Content, "save");
            boutons[8].Load(Content, "load");

            bg.Load(Content, "background");
            bgbutton.Load(Content, "bgbutton");
        }

        public static void Update(ref string typeObj, ref bool save, ref bool load)
        {
            mouse = Mouse.GetState();
            for (int i = 0; i < boutons.GetLength(0); i++)          
            {
                rec1.X = mouse.X;
                rec1.Y = mouse.Y;
                rec1.Width = 1;
                rec1.Height = 1;

                rec2.X = 700;
                rec2.Y = (int)boutons[i].Position.Y;
                rec2.Width = 100;
                rec2.Height = taille_case;

                if (Rectangle.Intersect(rec1, rec2) != Rectangle.Empty)
                {
                    bgbutton.Position.X = 700;
                    bgbutton.Position.Y = boutons[i].Position.Y - 5;

                    if (mouse.LeftButton == ButtonState.Pressed)
                    {
                        if (i >= 0 && i < 7)
                        {
                            if (i == 0)
                            {
                                typeObj = "C";
                            }
                            else if (i == 1)
                            {
                                typeObj = "o";
                            }
                            else if (i == 2)
                            {
                                typeObj = "<";
                            }
                            else if (i == 3)
                            {
                                typeObj = ">";
                            }
                            else if (i == 4)
                            {
                                typeObj = "s";
                            }
                            else if (i == 5)
                            {
                                typeObj = "h";
                            }
                            else if (i == 6)
                            {
                                typeObj = "e";
                            }
                        }
                        else if (i == 7)
                        {
                            save = true;
                        }
                        else if (i == 8)
                        {
                            load = true;
                        }
                    }
                }
            }
        }

        public static void Draw(SpriteBatch spriteBatch, GraphicsDeviceManager graphics)
        {
            bg.Position.X = graphics.PreferredBackBufferWidth - largeur;
            bg.Position.Y = 0;

            bg.Draw(spriteBatch);
            if (bgbutton.Position.X != -1 && bgbutton.Position.Y != -1)
            {
                bgbutton.Draw(spriteBatch);
            }
            
            for (int i = 0; i < boutons.GetLength(0); i++)
            {
                boutons[i].Position.X = graphics.PreferredBackBufferWidth - (largeur / 2) - (boutons[i].Texture.Width / 2);
                boutons[i].Position.Y = i * taille_case + 10 + i * 10;

                boutons[i].Draw(spriteBatch);
            }
        }
    }
}
